﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Data;
using System.Xml;

public partial class ws_CheckPoints : WSPageBase
{

    #region Page Events

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        // Enabled trace in code
        this.Context.Trace.IsEnabled = false;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        XmlDocument xReturn = getXML();
        Response.ContentType = "text/xml";
        Response.Write(xReturn.OuterXml);
    }

    #endregion

    #region Data

    public XmlDocument getXML()
    {
        XmlDocument xReturn = new XmlDocument();
        DataTable tbl = new DataTable("tbl");
        
        Exception err = new Ko2.DB.OneC().GetCheckPoints(ref tbl);

        if (err == null)
        {
            tbl.Columns[0].ColumnName = "ID";
            tbl.Columns[0].Caption = "ID";
            tbl.Columns[1].ColumnName = "Name";
            tbl.Columns[1].Caption = "Name";
        }
        else
        {
            tbl.Columns.Add("ID", typeof(string));
            tbl.Columns[0].Caption = "ID";
            tbl.Columns.Add("Name", typeof(string));
            tbl.Columns[1].Caption = "Name";

            DataRow r = tbl.NewRow();
            r[0] = "-1000";
            r[1] = "Ошибка получения данных.";
            tbl.Rows.Add(r);
            return xReturn;
        }

        using (StringWriter stringWriter = new StringWriter())
        {
            using (XmlTextWriter xmlTextwriter = new XmlTextWriter(stringWriter))
            {
                tbl.WriteXml(xmlTextwriter, XmlWriteMode.WriteSchema);
                //XmlNode xRoot = xReturn.CreateElement("root");
                //xReturn.AppendChild(xRoot);
                xReturn.LoadXml(stringWriter.ToString());

                foreach (XmlNode node0 in xReturn.ChildNodes)
                {
                    foreach (XmlNode node1 in node0.ChildNodes)
                    {
                        if (node1.Name == "tbl")
                        {
                            foreach (XmlNode node2 in node1.ChildNodes)
                            {
                                if (node2.Name == "Name")
                                {
                                    string shopnameTmp = node2.InnerText;
                                    DataTable tblData = new DataTable("row");
                                    err = new Ko2.DB.OneC().GetCheckPointsData(ref tblData, shopnameTmp, Year, Month);
                                    if (err == null)
                                    {
                                        using (StringWriter stringWriter2 = new StringWriter())
                                        {
                                            using (XmlTextWriter xmlTextwriter2 = new XmlTextWriter(stringWriter2))
                                            {
                                                tblData.WriteXml(xmlTextwriter2, XmlWriteMode.IgnoreSchema);
                                                XmlDocument xTmp = new XmlDocument();
                                                xTmp.LoadXml(stringWriter2.ToString());
                                                foreach (XmlNode xxx in xTmp.ChildNodes[0].ChildNodes)
                                                {
                                                    XmlNode rowNode = xReturn.CreateElement("row");
                                                    XmlNode rowItemDay = xReturn.CreateElement("Day");
                                                    rowItemDay.InnerText = xxx.ChildNodes[0].InnerText;
                                                    rowNode.AppendChild(rowItemDay);
                                                    XmlNode rowItemRealIn = xReturn.CreateElement("RealIn");
                                                    rowItemRealIn.InnerText = xxx.ChildNodes[1].InnerText;
                                                    rowNode.AppendChild(rowItemRealIn);
                                                    node1.AppendChild(rowNode);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }
        return xReturn;
    }

    #endregion

}